package com.denlaku.longan.controller.impl;

import com.denlaku.longan.controller.DashboardHisController;
import com.denlaku.longan.http.Resp;
import com.denlaku.longan.http.Return;
import com.denlaku.longan.qo.DashboardHisQuery;
import com.denlaku.longan.qo.Query;
import com.denlaku.longan.service.DashboardHisService;
import com.denlaku.longan.service.DashboardService;
import com.denlaku.longan.util.Objects;
import com.denlaku.longan.vo.Dashboard;
import com.denlaku.longan.vo.DashboardHis;
import com.denlaku.longan.vo.PageBy;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author tianx
 */
@RestController
public class DashboardHisControllerImpl implements DashboardHisController {
    @Autowired
    private DashboardHisService dashboardHisService;

    @Autowired
    private DashboardService dashboardService;

    /**
     * 删除仪表板历史记录
     * @param query 查询参数
     * @return 响应对象
     */
    @Override
    public Return<Integer> delete(DashboardHisQuery query) {
        dashboardHisService.delete(query);
        return Resp.success();
    }

    /**
     * 根据仪表板历史记录恢复仪表板
     * @param query 查询参数
     * @return 响应对象
     */
    @Override
    public Return<Void> recovery(DashboardHisQuery query) {
        DashboardHis dashboardHis = dashboardHisService.get(query);
        Dashboard dashboard = dashboardHis.getDashboard();
        Objects.ifPresent(dashboard, dashboardService::update);
        return Resp.success();
    }

    /**
     * 查询仪表板历史记录
     *
     * @param id 仪表板id
     * @return 响应对象
     */
    @Override
    public Return<Dashboard> get(Long id) {
        DashboardHis dashboardHis = dashboardHisService.get(Query.of(id));
        return Resp.success(Objects.map(dashboardHis, DashboardHis::getDashboard));
    }
    /**
     * 查询仪表板历史记录
     * @param query 查询参数
     * @return 响应对象
     */
    @Override
    public Return<List<DashboardHis>> list(@RequestBody DashboardHisQuery query, PageBy pageBy) {
        PageHelper.startPage(pageBy.getPage(), pageBy.getSize());
        List<DashboardHis> list = dashboardHisService.list(query);
        return Resp.success(list);
    }
}
